<script setup>
import {ref, defineEmits, defineProps, watchEffect, reactive} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
    //自定义属性负责接收外界传入的被修改的数据源
    const props = defineProps(["data"]);

    let form = reactive({
      id:"",
      courseName:"",
      videoFilename:"",
      image:"",
      originalPrice:"",
      currentPrice:"",
      description:"",
      targetAudience:"",
      learningOutcomes:"",
      subscriptionNotes:"",
      consultantId:"",
      listingTime:"",
      categoryId:"",
      date:""
    });

// 添加自定义事件
const emite = defineEmits(["editeValue"]);

watchEffect(() => {
  if (props.data) {
    Object.assign(form, props.data);
  }
});

async function saveAction() {
  try {
    console.log("-----------------------");
    console.log(form);

    // 发送 POST 请求到后端
    const response = await axios.post('/api/course/update', form);

    console.log('更新成功:', response.data);

    // 显示成功消息
    ElMessage.success('课程更新成功');

    // 清空表单属性
    Object.keys(form).forEach(key => {
      form[key] = "";
    });
  } catch (error) {
    console.error('更新失败:', error);

    // 显示错误消息
    ElMessage.error('课程更新失败');
  }
  emite("editeValue", form);
}

</script>

<template>
  <el-form :model="form">
    <!--          <el-form-item label="品牌序号：" label-width="100">-->
    <!--            <el-input v-model="form.id" :placeholder="请输入序号"></el-input>-->
    <!--          </el-form-item>-->
    <el-form-item label="课程名称：" label-width="100">
      <el-input v-model="form.courseName" :placeholder="请输入课程名称"></el-input>
    </el-form-item>
    <el-form-item label="视频文件：" label-width="100">
      <el-input v-model="form.videoFilename" :placeholder="请上传课程视频"></el-input>
    </el-form-item>

    <el-form-item label="课程图片：" label-width="100">
      <el-input v-model="form.image" :placeholder="请上传课程图片"></el-input>
    </el-form-item>

    <el-form-item label="原价：" label-width="100">
      <el-input v-model="form.originalPrice" :placeholder="请输入原价"></el-input>
    </el-form-item>
    <el-form-item label="现价：" label-width="100">
      <el-input v-model="form.currentPrice" :placeholder="请输入现价"></el-input>
    </el-form-item>
    <el-form-item label="课程简介：" label-width="100">
      <el-input v-model="form.description" :placeholder="请输入课程简介"></el-input>
    </el-form-item>
    <el-form-item label="适用人群：" label-width="100">
      <el-input v-model="form.targetAudience" :placeholder="请输入适用人群"></el-input>
    </el-form-item>
    <el-form-item label="学习收获：" label-width="100">
      <el-input v-model="form.learningOutcomes" :placeholder="请输入学习收获"></el-input>
    </el-form-item>
    <el-form-item label="订阅须知：" label-width="100">
      <el-input v-model="form.subscriptionNotes" :placeholder="请输入订阅须知"></el-input>
    </el-form-item>
    <el-form-item label="咨询师：" label-width="100">
      <el-input v-model="form.consultantId" :placeholder="请选择咨询师"></el-input>
    </el-form-item>
    <el-form-item label="有效期：" label-width="100">
      <el-input v-model="form.listingTime" :placeholder="请输入有效期"></el-input>
    </el-form-item>
    <el-form-item label="课程类别：" label-width="100">
      <el-input v-model="form.categoryId" :placeholder="请选择课程类别"></el-input>
    </el-form-item>

    <el-form-item label="创建时间：" label-width="100">
      <el-date-picker
          v-model="form.date"
          type="datetime"
          placeholder="请选择日期"
          size="normal"
          value-format="YYYY-MM-DD HH:mm:ss"
      />
    </el-form-item>
  </el-form>
  <div style="display: flex; justify-content: center;">
    <el-button @click="saveAction">保存</el-button>
  </div>
</template>

<style scoped lang="less">

</style>